package com.nopcomm.pages;

import org.openqa.selenium.By;

import com.nopcomm.abstractPage;

public class CreateAndEditConsumerPage extends abstractPage {
	public void createconsumer(String NewcustomerEmail,String NewCustomerpassword,String gender,
			String NewcustomerFirstname,String NewcustomerLastname,String NewcustomerDateofBirth) {
		//Click on Add new
				driver.findElement(By.linkText("Add new")).click();
				//Enter new Email id
				driver.findElement(By.id("Email")).sendKeys(NewcustomerEmail);
				//Enter password
				driver.findElement(By.id("Password")).sendKeys(NewCustomerpassword);
				//select gender
				if(gender.toLowerCase().equals("male")){
					driver.findElement(By.id("Gender_Male")).click();}
				else if (gender.toLowerCase().equals("female")){
				driver.findElement(By.id("Gender_Female")).click();}		
				//Firstname of customer
		       driver.findElement(By.id("FirstName")).sendKeys(NewcustomerFirstname);
		      //Lastname of Customer
		       driver.findElement(By.id("LastName")).sendKeys(NewcustomerLastname);
		      //Enter date of Birth
		      driver.findElement(By.id("DateOfBirth")).sendKeys(NewcustomerDateofBirth);
		 
		 
	}

	public static void selectroles(String RoleName) {
		//Select Roles Tab
		driver.findElement(By.linkText("Customer Roles")).click();
		//Select Roles
		if(RoleName.toLowerCase().equals("administrators")){
		driver.findElement(By.xpath("(//input[@name='SelectedCustomerRoleIds'])[1]")).click();}
		else if(RoleName.toLowerCase().equals("Forum Moderators")){
			driver.findElement(By.xpath("(//input[@name='SelectedCustomerRoleIds'])[2]")).click();}
		else if(RoleName.toLowerCase().equals("guests")){
			driver.findElement(By.xpath("(//input[@name='SelectedCustomerRoleIds'])[3]")).click();}
		else if(RoleName.toLowerCase().equals("registered")){
			driver.findElement(By.xpath("(//input[@name='SelectedCustomerRoleIds'])[4]")).click();}
		else if(RoleName.toLowerCase().equals("vendors")){
			driver.findElement(By.xpath("(//input[@name='SelectedCustomerRoleIds'])[5]")).click();}
	}

	public static void saveConsumer() {
		//Click to save
		driver.findElement(By.name("save")).click();
		
	}

	public static String getmessage() {
		// 
		
		return "The new customer has been added successfully.";
		
		
	}

	public void searchCustomer(String NewcustomerEmail, String RoleName) {
		//un select the default role
	    driver.findElement(By.xpath("(//input[@name='SearchCustomerRoleIds'])[3]")).click();
		//Select role
		if(RoleName.toLowerCase().equals("administrators")){
			driver.findElement(By.xpath("(//input[@name='SearchCustomerRoleIds'])[1]")).click();}
			else if(RoleName.toLowerCase().equals("forum Moderators")){
				driver.findElement(By.xpath("(//input[@name='SearchCustomerRoleIds'])[2]")).click();}
			else if(RoleName.toLowerCase().equals("guests")){
				driver.findElement(By.xpath("(//input[@name='SearchCustomerRoleIds'])[4]")).click();}
			else if(RoleName.toLowerCase().equals("registered")){
				driver.findElement(By.xpath("(//input[@name='SearchCustomerRoleIds'])[3]")).click();}
			else if(RoleName.toLowerCase().equals("vendors")){
				driver.findElement(By.xpath("(//input[@name='SearchCustomerRoleIds'])[5]")).click();}
		   // search with email
		   driver.findElement(By.name("SearchEmail")).sendKeys(NewcustomerEmail);
		//click on serach button
		driver.findElement(By.id("search-customers")).click();
		
	}

	public static boolean isEmailShown(String NewcustomerEmail) {
		// 
		return driver.getPageSource().contains(NewcustomerEmail);
	}

	public void editCustomer(String NewcustomerEmail) {
		// Click edit customer
		         try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(driver.findElement(By.linkText(NewcustomerEmail)).isDisplayed()){
				driver.findElement(By.xpath("(//a[contains(text(),'Edit')])")).click();}
				//change to inactive
				driver.findElement(By.id("Active")).click();
				//click Save
				driver.findElement(By.name("save")).click();

			}

	public boolean isMesgShown() {
		// 
		return driver.getPageSource().contains("The customer has been updated successfully.");
	}

	public void DeleteCustomer(String NewcustomerEmail) {
		// 
		// Click edit customer
        try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(driver.findElement(By.linkText(NewcustomerEmail)).isDisplayed()){
		driver.findElement(By.xpath("(//a[contains(text(),'Edit')])")).click();}
		//Click Delete
		driver.findElement(By.id("customer-delete")).click();
		//confirm delete
		driver.findElement(By.xpath("//input[@value='Delete']")).click();
	}

	public boolean isdeletedmesgShown() {
		// 
		return driver.getPageSource().contains("The customer has been deleted successfully.");
	}
	

}
